package com.omicrondev.flightops;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;
import java.util.ListIterator;

public class WeightBalance extends Activity {
    TextView toWeightBox, toArmBox;
    TextView ldgWeightBox, ldgArmBox;
    TextView zfWeightBox;
    LoadingGraph graph;
    AcType mAcType;
    FlightOps mApp;
    //ArrayAdapter mAdapter;
    //ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Toast.makeText(this, "WeightBalance onCreate", Toast.LENGTH_SHORT).show();

        mApp = (FlightOps)getApplicationContext();

        setContentView(R.layout.weight_balance);

        graph = (LoadingGraph)findViewById(R.id.loadingGraph);

        toWeightBox = (TextView)findViewById(R.id.toWeight);
        toArmBox = (TextView)findViewById(R.id.toCg);
        ldgWeightBox = (TextView)findViewById(R.id.ldgWeight);
        ldgArmBox = (TextView)findViewById(R.id.ldgCg);
        zfWeightBox = (TextView)findViewById(R.id.zfWeight);

        mAcType = mApp.mAcType;

        graph.setAcType(mAcType);
    }

    @Override
    protected void onStart() {
        super.onStart();

        //Toast.makeText(this, "WeightBalance onStart", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        //Toast.makeText(this, "WeightBalance onRestart", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //Toast.makeText(this, "WeightBalance onResume", Toast.LENGTH_SHORT).show();

        clearInputs();
        addInputs();

        updateDisplay();
    }

    @Override
    protected void onStop() {
        super.onStop();

        //Toast.makeText(this, "WeightBalance onStop", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        //Toast.makeText(this, "WeightBalance onRestoreInstanceState", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        //Toast.makeText(this, "WeightBalance onSaveInstanceState", Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.settings:
                //Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(this, EditAcType.class);
                //intent.putExtra("EXTRA_ID", "SOME DATAS");
                startActivity(intent);
                return true;
            case R.id.reset:
                mApp.removeData();
                Toast.makeText(this, "Reset", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void updateDisplay() {

        BigDecimal fuelQty, fuelWeight, fuelMoment;
        BigDecimal weight, moment;
        BigDecimal beWeight = new BigDecimal(mApp.mAcType.mBEW);
        BigDecimal beArm = new BigDecimal(mApp.mAcType.mBEA);
        BigDecimal beMoment = beWeight.multiply(beArm);
        BigDecimal toWeight, toArm, toMoment;

        toWeight = beWeight;
        toMoment = beMoment;

        fuelQty = fuelWeight = fuelMoment = BigDecimal.ZERO;

        ListIterator<CGStation> itr = mAcType.mStations.listIterator();
        CGStation station;
        while (itr.hasNext()) {
            station = itr.next();

            if (station.mIsFuel) {
                fuelQty = new BigDecimal(station.mValue);
                fuelWeight = weight = fuelQty.multiply(new BigDecimal(6));
                fuelMoment = moment = weight.multiply(new BigDecimal(station.mArm));
            }
            else {
                weight = new BigDecimal(station.mValue);
                moment = weight.multiply(new BigDecimal(station.mArm));
            }

            toWeight = toWeight.add(weight);
            toMoment = toMoment.add(moment);
        }

        // Takeoff

        toArm = toMoment.divide(toWeight, 1, BigDecimal.ROUND_HALF_UP);

        toWeightBox.setText(toWeight.toString());
        toArmBox.setText(toArm.toString());

        graph.setTO(toWeight.floatValue(), toArm.floatValue());

        if (toWeight.compareTo(new BigDecimal(4750)) == 1)
            toWeightBox.setTextColor(0xFFFF0000);
        else
            toWeightBox.setTextColor(0xFF000000);

        if (!InCgLimits(toWeight.intValue(), toArm.doubleValue()))
            toArmBox.setTextColor(0xFFFF0000);
        else
            toArmBox.setTextColor(0xFF000000);

        // Landing

        BigDecimal fuelUsed, fuelUsedWeight, fuelUsedMoment, ldgWeight, ldgArm, ldgMoment;
/*
        try {
            fuelUsed = new BigDecimal(fuelUsedBox.getText().toString());
            fuelUsedWeight = fuelUsed.multiply(new BigDecimal(6));
        } catch (NumberFormatException e) {
            fuelUsedWeight = fuelWeight;
        }
        fuelUsedMoment = fuelUsedWeight.multiply(new BigDecimal(93.6));
*/
        fuelUsedWeight = new BigDecimal(180);
        fuelUsedMoment = new BigDecimal(180 * 93.6);

        ldgWeight = toWeight.subtract(fuelUsedWeight);
        ldgMoment = toMoment.subtract(fuelUsedMoment);
        ldgArm = ldgMoment.divide(ldgWeight, 1, BigDecimal.ROUND_HALF_UP);

        ldgWeightBox.setText(ldgWeight.toString());
        ldgArmBox.setText(ldgArm.toString());

        graph.setLdg(ldgWeight.floatValue(), ldgArm.floatValue());

        if (ldgWeight.compareTo(new BigDecimal(4342)) == 1)
            ldgWeightBox.setTextColor(0xFFFF0000);
        else
            ldgWeightBox.setTextColor(0xFF000000);

        if (!InCgLimits(ldgWeight.intValue(), ldgArm.doubleValue()))
            ldgArmBox.setTextColor(0xFFFF0000);
        else
            ldgArmBox.setTextColor(0xFF000000);

        // Zero Fuel

        BigDecimal zfWeight;

        zfWeight = toWeight.subtract(fuelWeight);

        zfWeightBox.setText(zfWeight.toString());

        if (zfWeight.compareTo(new BigDecimal(4000)) == 1)
            zfWeightBox.setTextColor(Color.RED);
        else
            zfWeightBox.setTextColor(Color.BLACK);

    }

    private boolean InCgLimits(int weight, double arm)
    {
        double cgLimit;

        cgLimit = mApp.mAcType.getFwdLimit(weight);
        if ((cgLimit == 0) || (arm < cgLimit))
            return false;

        cgLimit = mApp.mAcType.getAftLimit(weight);
        if ((cgLimit == 0) || (arm > cgLimit))
            return false;

        return true;
    }

    protected void clearInputs() {
        LinearLayout inputs = (LinearLayout)findViewById(R.id.inputs);

        inputs.removeAllViews();
    }

    protected void addInputs() {
        LinearLayout inputs = (LinearLayout)findViewById(R.id.inputs);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ListIterator<CGStation> itr = mAcType.mStations.listIterator();
        CGStation station;
        while (itr.hasNext()) {
            station = itr.next();

            View view = inflater.inflate(R.layout.listitem, null);


            TextView name = (TextView)(view.findViewById(R.id.name));
            name.setText(station.mName);

            EditText input = (EditText)(view.findViewById(R.id.input));
            input.setText(String.valueOf(station.mValue));
            input.setTag(station);

            View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (!hasFocus) {
                        EditText input = (EditText)view;
                        BigDecimal value;
                        try {
                            value = new BigDecimal(input.getText().toString());
                        } catch (NumberFormatException e) {
                            value = new BigDecimal(0);
                        }

                        CGStation station = (CGStation)(view.getTag());
                        station.mValue = value.intValue();

                        updateDisplay();
                    }
                }
            };

            input.setOnFocusChangeListener(focusChangeListener);

            inputs.addView(view);
        }
    }
}
